from game import zz_game
from zz import zz_log, zz_img

log = zz_log.of()


# 山海北荒卷

class Game(zz_game.ZZ_Game):
    def __init__(self, d):
        super().__init__(d)

    def get_name(self):
        return "山海北荒卷"

    def game_alert(self):
        self.alert_close_by_file("./game/img/山海北荒卷", 0.9)

    def egg_stop(self):
        self.log_msg("点击停止孵蛋")
        self.d.click(632, 1974)
        self.d.sleep(1.5)

        self.play_alert()

    def play_alert(self):
        if not self.img_exit("./game/img/山海北荒卷/alert.png", 3):
            self.log_msg("不存在弹窗")
            return

        if self.img_exit("./game/img/山海北荒卷/egg_state.png", 3):
            self.log_msg("战力上升，点击收服")
            self.img_click("./game/img/山海北荒卷/egg_get.png")
        else:
            self.log_msg("战力下降，点击出售")
            self.img_click("./game/img/山海北荒卷/egg_buy.png")
        self.egg_auto()

    def egg_auto(self):
        """
        自动孵蛋
        :return:
        """
        self.img_click_if_exists('点击自动孵蛋', "./game/img/山海北荒卷/auto_egg.png", "./game/img/山海北荒卷/start_egg.jpg")
        self.log_msg("点击开始孵蛋")
        self.img_click("./game/img/山海北荒卷/start_egg.jpg")

    def fu_di_prize(self):
        """
        彩福
        """
        fu_prize_list = [
            "./game/img/山海北荒卷/fu_prize_00.png",
            "./game/img/山海北荒卷/fu_prize_01.png",
            "./game/img/山海北荒卷/fu_prize_02.png",
            "./game/img/山海北荒卷/fu_prize_03.png",
            "./game/img/山海北荒卷/fu_prize_04.png",
        ]
        ok = False
        for fu_prize in fu_prize_list:
            prize_name = fu_prize.split("/")[-1]
            r = self.find_img(fu_prize, 2)
            if r.err():
                self.log_msg(f'采福未找到：{prize_name}')
                continue
            x, y = r.get_data("center")
            if x < 290:
                self.log_msg(f'采福物品靠近任务弹窗，pass：{prize_name}')
                continue

            ok = True
            self.img_click_if_exists(f'采福已找到：{prize_name}，开始采集', fu_prize, './game/img/山海北荒卷/fu_qiang_alert.png')
            self.log_msg(f'点击采集')
            self.img_click('./game/img/山海北荒卷/fu_qiang.png')
            break
        if ok:
            # self.img_click_if_exists(f'采集成功，开始召回', './game/img/山海北荒卷/fu_return_index.png', './game/img/山海北荒卷/fu_return.png')
            self.log_msg(f'采集成功，开始召回')
            self.d.click(600, 2250)
            self.log_msg(f'点击召回')
            self.img_click('./game/img/山海北荒卷/fu_return.png')
            self.log_msg("点击取消弹窗")
            self.d.click(600, 2300)
            self.d.sleep(2)

        return ok

    def fu_di(self):
        """
        天地福地
        """
        self.img_click_if_exists('点击进入洞府', "./game/img/山海北荒卷/fu_dong.png", "./game/img/山海北荒卷/fu_dong_01.png")
        self.img_click_if_exists('点击福地洞天', "./game/img/山海北荒卷/fu_dong_01.png", "./game/img/山海北荒卷/fu_qiang_index.png")
        if self.img_exit("./game/img/山海北荒卷/fu_other.png", 2):
            self.log_msg("在别人福地，点击回归")
            self.img_click("./game/img/山海北荒卷/fu_other.png")

        for i in range(3):
            self.log_msg(f"{i}-开始采福，先判断自己福地")
            ok = self.fu_di_prize()
            if ok:
                self.log_msg(f"{i}-采福成功")
                continue
            self.img_click_if_exists(f"{i}-采福失败，点击抢夺", "./game/img/山海北荒卷/fu_qiang_index.png", "./game/img/山海北荒卷/fu_qiang_go.png")
            self.log_msg("点击前往别人福地")
            self.img_click("./game/img/山海北荒卷/fu_qiang_go.png")
            ok = self.fu_di_prize()
            if ok:
                self.log_msg(f"{i}-采福成功")
                continue
            self.log_msg("采福失败，退出")
            break

        self.log_msg("退出天地福地")
        self.img_click("./game/img/山海北荒卷/fu_back.png")
        self.log_msg("点击取消弹窗")
        self.d.click(900, 1500)

    def handle_main(self):
        # 点击进入游戏位置
        if not self.img_click("./game/img/山海北荒卷/btn_start.jpg"):
            return

        self.img_exit("./game/img/山海北荒卷/mijing.jpg", 100)
        self.log_msg("已进入游戏主界面，开始扫荡")
        # 扫荡boss，获取蛋
        self.img_click_if_exists('扫荡--点击秘境', "./game/img/山海北荒卷/mijing.jpg", "./game/img/山海北荒卷/boss.jpg")
        self.img_click_if_exists('扫荡--点击野外首领', "./game/img/山海北荒卷/boss.jpg", "./game/img/山海北荒卷/scan.jpg")
        self.img_click_if_exists('扫荡--点击扫荡', "./game/img/山海北荒卷/scan.jpg", "./game/img/山海北荒卷/scan01.jpg")
        self.log_msg("扫荡--点击扫荡确认")
        if not self.img_click("./game/img/山海北荒卷/scan01.jpg"):
            return
        self.log_msg("扫荡成功，返回游戏首页")
        self.click_bottom()
        self.click_bottom()
        self.click_bottom()
        self.click_bottom()
        self.d.sleep(1)

        # if not self.fu_check(1):
        #     self.log_msg("福地采福")
        #     self.fu_di()
        # else:
        #     self.log_msg("福地采福-已完成")

        self.egg_stop()
        self.log_msg("开始孵蛋")
        self.egg_auto()

        index = 1
        while True:
            if self.fu_check(2):
                self.log_msg("任务已完成")
                break

            if index % 3 == 0:
                # 每隔一段时间停止，防止一直停住
                self.game_alert()
                self.egg_stop()

            index += 1
            self.d.sleep(30)

        self.d.sleep(2)
        self.get_fu()
        return True
